﻿<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:System="clr-namespace:System;assembly=mscorlib"
                    xmlns:Controls="clr-namespace:MahApps.Metro.Controls"
                    xmlns:Converters="clr-namespace:MahApps.Metro.Converters">

    <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
    <Converters:ThicknessToDoubleConverter x:Key="ThicknessToDoubleConverter" />

    <Style x:Key="MetroDatePicker" TargetType="{x:Type DatePicker}">
        <Setter Property="Background" Value="{DynamicResource ControlBackgroundBrush}" />
        <Setter Property="BorderBrush" Value="{DynamicResource TextBoxBorderBrush}" />
        <Setter Property="BorderThickness" Value="1" />
        <Setter Property="Controls:TextboxHelper.IsMonitoring" Value="True" />
        <Setter Property="FontFamily" Value="{DynamicResource ContentFontFamily}" />
        <Setter Property="FontSize" Value="{DynamicResource ContentFontSize}" />
        <Setter Property="Foreground" Value="{DynamicResource TextBrush}" />
        <Setter Property="MinHeight" Value="26" />
        <Setter Property="HorizontalContentAlignment" Value="Stretch" />
        <Setter Property="IsTodayHighlighted" Value="True" />
        <Setter Property="Padding" Value="0" />
        <Setter Property="SelectedDateFormat" Value="Short" />
        <Setter Property="SnapsToDevicePixels" Value="True" />
        <Setter Property="CalendarStyle" Value="{DynamicResource MetroCalendar}"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type DatePicker}">
                    <Grid x:Name="PART_Root">
                        <Grid.Resources>
                            <SolidColorBrush x:Key="DisabledBrush" Color="#A5FFFFFF" />
                        </Grid.Resources>
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="CommonStates">
                                <VisualState x:Name="Normal" />
                                <VisualState x:Name="Disabled">
                                    <Storyboard>
                                        <DoubleAnimation Duration="0" To="1" Storyboard.TargetProperty="Opacity"
                                                         Storyboard.TargetName="PART_DisabledVisual" />
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                            <VisualStateGroup x:Name="ValidationStates">
                                <VisualState x:Name="Valid" />
                                <VisualState x:Name="InvalidUnfocused">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility" Storyboard.TargetName="ValidationErrorElement">
                                            <DiscreteObjectKeyFrame KeyTime="0">
                                                <DiscreteObjectKeyFrame.Value>
                                                    <Visibility>Visible</Visibility>
                                                </DiscreteObjectKeyFrame.Value>
                                            </DiscreteObjectKeyFrame>
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="InvalidFocused">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility" Storyboard.TargetName="ValidationErrorElement">
                                            <DiscreteObjectKeyFrame KeyTime="0">
                                                <DiscreteObjectKeyFrame.Value>
                                                    <Visibility>Visible</Visibility>
                                                </DiscreteObjectKeyFrame.Value>
                                            </DiscreteObjectKeyFrame>
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="IsOpen" Storyboard.TargetName="validationTooltip">
                                            <DiscreteObjectKeyFrame KeyTime="0">
                                                <DiscreteObjectKeyFrame.Value>
                                                    <System:Boolean>True</System:Boolean>
                                                </DiscreteObjectKeyFrame.Value>
                                            </DiscreteObjectKeyFrame>
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                        <Rectangle x:Name="Base" Stroke="{TemplateBinding BorderBrush}"
                                   StrokeThickness="{TemplateBinding BorderThickness, Converter={StaticResource ThicknessToDoubleConverter}}"
                                   Opacity="1" Fill="{TemplateBinding Background}" />
                        <Rectangle x:Name="FocusRectangle"
                                   StrokeThickness="{TemplateBinding BorderThickness, Converter={StaticResource ThicknessToDoubleConverter}}"
                                   Opacity="0" Stroke="{DynamicResource BlackBrush}" />
                        <Grid Margin="2">
                            <Button x:Name="PART_Button" Style="{DynamicResource ChromelessButtonStyle}"
                                    Foreground="{TemplateBinding Foreground}" Width="20" HorizontalAlignment="Right"
                                    IsTabStop="False">
                                <Path Width="12" Height="12" Stretch="Fill"
                                      Fill="{Binding ElementName=PART_Button, Path=Foreground}"
                                      Data="F1 M 11.0017,36.005L 11.0017,14.002L 14.0021,14.002L 14.0021,11.0015L 19.0028,11.0015L 19.0028,14.002L 28.0039,14.002L 28.0039,11.0015L 33.0046,11.0015L 33.0046,14.002L 36.005,14.002L 36.005,36.005L 11.0017,36.005 Z M 13.0019,34.0048L 34.0047,34.0048L 34.0047,19.0027L 13.0019,19.0027L 13.0019,34.0048 Z M 16.5024,12.5017C 15.95,12.5017 15.5022,13.1734 15.5022,14.0019C 15.5022,14.8304 15.95,15.5021 16.5024,15.5021C 17.0547,15.5021 17.5025,14.8304 17.5025,14.0019C 17.5025,13.1734 17.0547,12.5017 16.5024,12.5017 Z M 30.5,12.4998C 29.9476,12.4998 29.4999,13.1715 29.4999,14C 29.4999,14.8286 29.9476,15.5002 30.5,15.5002C 31.0524,15.5002 31.5001,14.8286 31.5001,14C 31.5001,13.1715 31.0524,12.4998 30.5,12.4998 Z M 20.5994,33.0046L 18.1154,33.0046L 18.1154,23.7987C 17.2116,24.7375 16.1471,25.4314 14.9217,25.8803L 14.9217,23.3868C 15.5686,23.179 16.2709,22.7857 17.0287,22.2069C 17.7864,21.628 18.3067,20.9527 18.5895,20.1809L 20.5994,20.1809L 20.5994,33.0046 Z M 27.874,33.0046L 27.874,30.5111L 22.7286,30.5111L 22.7286,28.3822L 28.2039,20.359L 30.358,20.359L 30.358,28.3738L 31.9548,28.3738L 31.9548,30.5111L 30.358,30.5111L 30.358,33.0046L 27.874,33.0046 Z M 27.874,28.3738L 27.874,24.2551L 25.0601,28.3738L 27.874,28.3738 Z " />
                            </Button>

                            <DatePickerTextBox x:Name="PART_TextBox" Foreground="{TemplateBinding Foreground}"
                                               Focusable="{TemplateBinding Focusable}"
                                               HorizontalContentAlignment="Stretch"
                                               VerticalContentAlignment="Stretch"/>

                            <Rectangle x:Name="PART_DisabledVisual"
                                   Stroke="{DynamicResource ControlsDisabledBrush}"
                                   StrokeThickness="{TemplateBinding BorderThickness, Converter={StaticResource ThicknessToDoubleConverter}}"
                                   Fill="{DynamicResource ControlsDisabledBrush}"
                                   IsHitTestVisible="False"
                                   Opacity="0" />
                            <Popup x:Name="PART_Popup" AllowsTransparency="True" Placement="Bottom" PlacementTarget="{Binding ElementName=PART_Root}" StaysOpen="False"/>
                            <Border x:Name="ValidationErrorElement" BorderBrush="{DynamicResource ControlsValidationBrush}" BorderThickness="1" Visibility="Collapsed">
                                <ToolTipService.ToolTip>
                                    <ToolTip x:Name="validationTooltip"
                                         DataContext="{Binding RelativeSource={RelativeSource TemplatedParent}}"
                                         Placement="Right"
                                         PlacementTarget="{Binding RelativeSource={RelativeSource TemplatedParent}}"
                                         Template="{DynamicResource ValidationToolTipTemplate}">
                                        <ToolTip.Triggers>
                                            <EventTrigger RoutedEvent="Canvas.Loaded">
                                                <BeginStoryboard>
                                                    <Storyboard>
                                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="IsHitTestVisible">
                                                            <DiscreteObjectKeyFrame KeyTime="0">
                                                                <DiscreteObjectKeyFrame.Value>
                                                                    <System:Boolean>true</System:Boolean>
                                                                </DiscreteObjectKeyFrame.Value>
                                                            </DiscreteObjectKeyFrame>
                                                        </ObjectAnimationUsingKeyFrames>
                                                    </Storyboard>
                                                </BeginStoryboard>
                                            </EventTrigger>
                                        </ToolTip.Triggers>
                                    </ToolTip>
                                </ToolTipService.ToolTip>
                                <Grid Background="Transparent" HorizontalAlignment="Right" Height="12" Margin="1,-4,-4,0" VerticalAlignment="Top" Width="12">
                                    <Path Data="M 1,0 L6,0 A 2,2 90 0 1 8,2 L8,7 z" Fill="{DynamicResource ValidationBrush5}" Margin="1,3,0,0" />
                                    <Path Data="M 0,0 L2,0 L 8,6 L8,8" Fill="{DynamicResource WhiteColorBrush}" Margin="1,3,0,0" />
                                </Grid>
                            </Border>
                        </Grid>
                    </Grid>

                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter TargetName="FocusRectangle" Property="Opacity" Value="1" />
                        </Trigger>

                        <Trigger Property="IsKeyboardFocusWithin" Value="True">
                            <Setter TargetName="FocusRectangle" Property="Opacity" Value="1" />
                        </Trigger>

                        <Trigger SourceName="PART_Button" Property="IsMouseOver" Value="True">
                            <Setter TargetName="PART_Button" Property="Background" Value="{DynamicResource GrayBrush5}" />
                        </Trigger>
                        
                        <Trigger SourceName="PART_Button" Property="IsPressed" Value="True">
                            <Setter TargetName="PART_Button" Property="Background" Value="{DynamicResource BlackBrush}" />
                            <Setter TargetName="PART_Button" Property="Foreground" Value="{DynamicResource WhiteBrush}" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <Style x:Key="MetroDatePickerTextBox" TargetType="{x:Type DatePickerTextBox}">
        <Setter Property="Controls:TextboxHelper.IsMonitoring" Value="True" />
        <Setter Property="Background" Value="{DynamicResource ControlBackgroundBrush}" />
        <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
        <Setter Property="FontFamily" Value="{DynamicResource ContentFontFamily}" />
        <Setter Property="FontSize" Value="{DynamicResource ContentFontSize}" />
        <Setter Property="Foreground" Value="{DynamicResource TextBrush}" />
        <Setter Property="ScrollViewer.PanningMode" Value="VerticalFirst" />
        <Setter Property="Stylus.IsFlicksEnabled" Value="False" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type DatePickerTextBox}">
                    <ControlTemplate.Resources>
                        <Storyboard x:Key="EnterGotFocus">
                            <DoubleAnimation Duration="0:0:0.2" To=".2" Storyboard.TargetProperty="Opacity"
                                             Storyboard.TargetName="PART_Watermark" />
                            <DoubleAnimation Duration="0:0:0.2" To=".2" Storyboard.TargetProperty="Opacity"
                                             Storyboard.TargetName="Message" />
                        </Storyboard>
                        <Storyboard x:Key="ExitGotFocus">
                            <DoubleAnimation Duration="0:0:0.2" Storyboard.TargetProperty="Opacity"
                                             Storyboard.TargetName="PART_Watermark" />
                            <DoubleAnimation Duration="0:0:0.2" Storyboard.TargetProperty="Opacity"
                                             Storyboard.TargetName="Message" />
                        </Storyboard>

                        <Storyboard x:Key="EnterHasText">
                            <DoubleAnimation Duration="0:0:0.2" From=".2" To="0" Storyboard.TargetProperty="Opacity"
                                             Storyboard.TargetName="PART_Watermark" />
                            <DoubleAnimation Duration="0:0:0.2" From=".2" To="0" Storyboard.TargetProperty="Opacity"
                                             Storyboard.TargetName="Message" />
                        </Storyboard>
                        <Storyboard x:Key="ExitHasText">
                            <DoubleAnimation Duration="0:0:0.2" Storyboard.TargetProperty="Opacity"
                                             Storyboard.TargetName="PART_Watermark" />
                            <DoubleAnimation Duration="0:0:0.2" Storyboard.TargetProperty="Opacity"
                                             Storyboard.TargetName="Message" />
                        </Storyboard>
                    </ControlTemplate.Resources>
                    <Grid>

                        <ScrollViewer x:Name="PART_ContentHost" BorderThickness="0" IsTabStop="False" Margin="2,2,20,2"
                                      VerticalAlignment="Stretch" Background="{x:Null}" FocusVisualStyle="{x:Null}"/>
                        <ContentControl x:Name="PART_Watermark" Focusable="False" IsHitTestVisible="False"
                                        Opacity="0.6" Padding="2" Margin="6,0,0,0" HorizontalAlignment="Left"
                                        VerticalAlignment="Center" VerticalContentAlignment="Center"
                                        Foreground="{TemplateBinding Foreground}" 
                                        Visibility="Hidden"/>
                        <TextBlock x:Name="Message" Text="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DatePicker}}, Path=(Controls:TextboxHelper.Watermark)}"
                                       Foreground="{TemplateBinding Foreground}" IsHitTestVisible="False" Opacity="0.6" HorizontalAlignment="Left" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                       Margin="4,0,0,0" Padding="2"/>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <MultiTrigger>
                            <MultiTrigger.Conditions>
                                <Condition Property="Controls:TextboxHelper.HasText" Value="False" />
                                <Condition Property="IsFocused" Value="True" />
                            </MultiTrigger.Conditions>
                            <MultiTrigger.EnterActions>
                                <BeginStoryboard Storyboard="{StaticResource EnterGotFocus}" />
                            </MultiTrigger.EnterActions>
                            <MultiTrigger.ExitActions>
                                <BeginStoryboard Storyboard="{StaticResource ExitGotFocus}" />
                            </MultiTrigger.ExitActions>
                        </MultiTrigger>

                        <DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DatePicker}}, Path=(Controls:TextboxHelper.Watermark)}" Value="{x:Static System:String.Empty}">
                            <Setter TargetName="PART_Watermark" Property="Visibility" Value="Visible"/>
                        </DataTrigger>
                        
                        <Trigger Property="Controls:TextboxHelper.HasText" Value="True">
                            <Trigger.EnterActions>
                                <BeginStoryboard Storyboard="{StaticResource EnterHasText}" />
                            </Trigger.EnterActions>
                            <Trigger.ExitActions>
                                <BeginStoryboard Storyboard="{StaticResource ExitHasText}" />
                            </Trigger.ExitActions>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

</ResourceDictionary>